from django.shortcuts import render, HttpResponse
from django.views.generic import View, ListView, TemplateView
from resources.models import *
from django.http.response import JsonResponse
import json


# Create your views here.
class IdcListView(ListView):
    template_name = 'idc_list.html'
    model = Idc


class IdcAddView(TemplateView):
    template_name = 'idc_add.html'

    def post(self, request):
        res = {'status': 0, 'msg': '添加机房成功'}
        try:
            idc = Idc()
            idc.name = request.POST.get('name')
            idc.name_cn = request.POST.get('name_cn')
            idc.address = request.POST.get('address')
            idc.phone = request.POST.get('phone')
            idc.username = request.POST.get('username')
            idc.username_email = request.POST.get('username_email')
            idc.username_phone = request.POST.get('username_phone')
            idc.save()
        except Exception as e:
            print(e)
            res = {'status': 1, 'msg': '添加机房失败'}
        return JsonResponse(res)


class IdcUpdateView(View):
    def get(self, request):
        idc_obj = Idc.objects.get(id=request.GET.get('id'))
        return render(request, 'idc_update.html', locals())

    def post(self, request):
        res = {'status': 0, 'msg': '更新机房成功'}
        try:
            idc = Idc.objects.get(id=request.POST.get('id'))
            idc.name = request.POST.get('name')
            idc.name_cn = request.POST.get('name_cn')
            idc.address = request.POST.get('address')
            idc.phone = request.POST.get('phone')
            idc.username = request.POST.get('username')
            idc.username_email = request.POST.get('username_email')
            idc.username_phone = request.POST.get('username_phone')
            idc.save()
        except Exception as e:
            print(e)
            res = {'status': 1, 'msg': '更新机房失败'}
        return JsonResponse(res)


class IdcDeleteView(View):
    def post(self, request):
        res = {'status': 0, 'msg': '删除机房成功'}
        try:
            Idc.objects.get(id=request.POST.get('id')).delete()
        except Exception as e:
            print(e)
            res = {'status': 1, 'msg': '删除机房失败'}
        return JsonResponse(res)


class ServerUserListView(ListView):
    template_name = 'serveruser_list.html'
    model = ServerUser


class ServerUserAddView(TemplateView):
    template_name = 'serveruser_add.html'

    def post(self, request):
        res = {'status': 0, 'msg': '添加资产用户成功'}
        try:
            serveruser = ServerUser()
            serveruser.name = request.POST.get('name')
            serveruser.username = request.POST.get('username')
            serveruser.password = request.POST.get('password')
            serveruser.info = request.POST.get('info')
            serveruser.save()
        except Exception as e:
            print(e)
            res = {'status': 1, 'msg': '添加资产用户失败'}
        return JsonResponse(res)


class ServerUserUpdateView(View):
    def get(self, request):
        serveruser_obj = ServerUser.objects.get(id=request.GET.get('id'))
        return render(request, 'serveruser_update.html', locals())

    def post(self, request):
        res = {'status': 0, 'msg': '更新资产用户成功'}
        try:
            serveruser = ServerUser.objects.get(id=request.POST.get('id'))
            serveruser.name = request.POST.get('name')
            serveruser.username = request.POST.get('username')
            serveruser.password = request.POST.get('password')
            serveruser.info = request.POST.get('info')
            serveruser.save()
        except Exception as e:
            print(e)
            res = {'status': 1, 'msg': '更新资产用户失败'}
        return JsonResponse(res)


class ServerUserDeleteView(View):
    def post(self, request):
        res = {'status': 0, 'msg': '删除资产用户成功'}
        try:
            ServerUser.objects.get(id=request.POST.get('id')).delete()
        except Exception as e:
            print(e)
            res = {'status': 1, 'msg': '删除资产失败'}
        return JsonResponse(res)


class ServerListView(ListView):
    template_name = 'server_list.html'
    model = Server



class ServerDataApiView(View):
    def post(self, request):
        data_dict = json.loads(request.body)
        # print(data_dict)
        # 添加数据
        # 主机基本信息
        res = {'status': 0, 'msg': 'success'}
        # 判断是否存在数据
        try:
            # 存在就修改
            server = Server.objects.get(uuid=data_dict['uuid'])
        except Exception as e:
            # print(e)
            # 不存在添加
            server = Server()
        try:
            server.hostname = data_dict['hostname']
            server.cpu_info = data_dict['cpu_info']
            server.cpu_count = data_dict['cpu_count']
            server.mem_info = data_dict['mem_info']
            server.os_system = data_dict['os_system']
            server.os_system_num = data_dict['os_system_num']
            server.uuid = data_dict['uuid']
            server.sn = data_dict['sn']
            server.scan_status = 1
            server.save()
            # 磁盘
            for k, v in data_dict['disk_info'].items():
                try:
                    disk = Disk.objects.get(server_id=server.id, name=k)
                except Exception as e:
                    print(e)
                    disk = Disk()
                disk.name = k
                disk.size = v
                disk.server = server
                disk.save()
            # 网卡
            for k, v in data_dict['ip_info'].items():
                try:
                    network = NetWork.objects.get(server_id=server.id, name=k)
                except Exception as e:
                    print(e)
                    network = NetWork()
                network.name = k
                network.ip_address = v
                network.server = server
                network.save()
        except Exception as e:
            print(e)
            res = {'status': 1, 'msg': e}
        return JsonResponse(res)
